function getPath(){
	var strFullPath=window.document.location.href;  
    var strPath=window.document.location.pathname;  
    var pos=strFullPath.indexOf(strPath);  
    var prePath=strFullPath.substring(0,pos);  
    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);  
    var basePath = prePath;  
    basePath = prePath + postPath;
    return basePath;
}
function getPrePath(){
	var strFullPath=window.document.location.href;  
    var strPath=window.document.location.pathname;  
    var pos=strFullPath.indexOf(strPath);  
    var prePath=strFullPath.substring(0,pos);  
    var postPath=strPath.substring(0,strPath.substr(1).indexOf('/')+1);  
    var basePath = prePath;  
    return basePath;
}
var debug = true;
var serverUrl=getPath();
var qrcodeUrl="http://www.shinehao.net/weixin/weiXin/sh?orderId=";
var imgUrl = getPrePath();
var isChrome = navigator.userAgent.toLowerCase().match(/chrome/) != null;
if(!isChrome){
    $('#dialogforsysinfo').dialogBox({
            type: 'error',  //three type:'normal'(default),'correct','error',
            width: 300,
            height: 300,
            hasMask: true,
            autoHide: true,
            hasClose:true,
            time: 3000,
            effect: 'fall',//fall
            title: '提醒',
            content: "为了提高软件使用体验,请使用谷歌Chrome浏览器,3秒之后跳转Chrome下载页,即将关闭此页面"
        });
    setTimeout(function(){
    			newWinNoRepeat("http://rj.baidu.com/soft/detail/14744.html?ald","getChrome"); 
                window.close();
 			},3000)
    
}

var basicDataFixed={
    username:"",
    company:"",
    order_no:"",
    is_update:"",//1只能下拉选择
    init:function(){
        $.ajax({
              type: 'POST',
              url: serverUrl+"/createOrderBusiness/getOrderInfoFixed",
              data: {},
              success: function(data){
               if(!data.result){
                   show_error_dialog(data.info);
                   return;
               }
               console.info(data.data[0].result);
               console.info(data.data[1].loginCompany);
               basicDataFixed.order_no = data.data[0].result;
               basicDataFixed.company  = data.data[1].loginCompany;
               basicDataFixed.username  = data.data[1].loginUserName;
               basicDataFixed.isUpdate  = data.data[2].isUpdate;
              },
              dataType: "json"
            });
    }
}
basicDataFixed.init();
/*if(window.location.href.indexOf("template.html")>-1){
   myformUtils.formClearAll("#myformsubmit","order_");
   basic_index.template_id=localStorage.getItem("template_id");
}*/

//动态添加订单编号
if($("[name='order_p_order_no']").length==0){
	$("#myformsubmit").prepend('<input type="text" name="order_p_order_no" readonly="readonly" hidden>')
}
//小工具
var menubuttonIndexTool={
	interval_saveOrderInfo:'',
	saveAsimg:function(){
		$.getScript("./jquery/html2img/html2cavas.min.js")
		 .done(function() {
			 $.getScript("./jquery/html2img/canvas2image.js")
			 .done(function() {
				 html2canvas(document.querySelector(".right")).then(canvas => {
						//console.info(canvas.toDataURL("image/webp", "1.0")); 
					    Canvas2Image.saveAsImage(canvas, canvas.width, canvas.height, 'jpeg',basicDataFixed.company+"-"+$("[name='order_p_order_no']").val());
					});
			});
		});
	},
	//获取rightbase64img
	saveAsBase64img:function(){
		/*$.getScript("./jquery/html2img/html2cavas.min.js")
		 .done(function() {
			 $.getScript("./jquery/html2img/canvas2image.js")
			 .done(function() {
				 html2canvas(document.querySelector(".right")).then(canvas => {
						console.info(canvas.toDataURL("image/jpeg", "1.0")); 
						menubuttonIndexTool.saveAsBase64imgAjax(canvas.toDataURL("image/jpeg", "1.0"));
					});
			});
		});*/
		html2canvas(document.querySelector(".right")).then(canvas => {
			console.info(canvas.toDataURL("image/jpeg", "1.0")); 
			menubuttonIndexTool.saveAsBase64imgAjax(canvas.toDataURL("image/jpeg", "1.0"));
		});
	},
	//base64 img保存于服务器
	saveAsBase64imgAjax:function(base64img){
		$.ajax({
	        type: 'POST',
	        url: serverUrl+"/fileManager/uploadFileByBase64",
	        data: {base64content:base64img,order_no:$("[name='order_p_order_no']").val(),type:1},
	        success: function(data){
	          console.info(data);
	          console.info("menubuttonIndexTool.saveAsBase64imgAjax success");
	        }
		});
	},
	//template.html新增订单实时保存数据
	saveTemplateDataAsFile:function(){
		if(window.location.href.indexOf("template.html")>-1&&window.location.href.indexOf("order_no")<0){
			var data=myformUtils.formToArrByAllNames("#myformsubmit","order_");
			var time = utilgetFormatDate("yyyy-MM-dd HH:mm:SS");
			$.getScript("./jquery/html5file/FileSaver20141129.js")
			 .done(function() {
				 var blob = new Blob([JSON.stringify(data)], {type: "text/plain;charset=utf-8"});
				 saveAs(blob, time+"-订单.json");
				 /*$.messager.show({
			            title:'提示',
			            msg:'自动保存成功下载完毕,文件名['+time+"-订单.json]",
			            timeout:1000,
			            showType:'slide',
			            style:{
		                    right:'',
		                    bottom:''
		                }
			     });*/
			});
		}
	},
	//template.html新增订单实时保存数据
	saveTemplateDataAsFileNow:function(){
		if(window.location.href.indexOf("template.html")>-1){
			var data=myformUtils.formToArrByAllNames("#myformsubmit","order_");
			var time = utilgetFormatDate("yyyy-MM-dd HH:mm:SS");
			$.getScript("./jquery/html5file/FileSaver20141129.js")
			 .done(function() {
				 var blob = new Blob([JSON.stringify(data)], {type: "text/plain;charset=utf-8"});
				 saveAs(blob, time+"-订单.json");
				 /*$.messager.show({
			            title:'提示',
			            msg:'自动保存成功下载完毕,文件名['+time+"-订单.json]",
			            timeout:1000,
			            showType:'slide',
			            style:{
		                    right:'',
		                    bottom:''
		                }
			     });*/
			});
		}
	},
	//禁止自动保存
	clear_interval_saveOrderInfo:function(){
		clearInterval(menubuttonIndexTool.interval_saveOrderInfo);
		$.messager.show({
            title:'提示',
            msg:'已禁止自动保存',
            timeout:2000,
            showType:'slide',
        	style:{
                right:'',
                bottom:''
            }
        });
	},
	//启用自动保存
	start_interval_saveOrderInfo:function(){
		//每三分钟保存本地文件
		clearInterval(menubuttonIndexTool.interval_saveOrderInfo);
		menubuttonIndexTool.interval_saveOrderInfo = setInterval(function(){menubuttonIndexTool.saveTemplateDataAsFile()},180000);
	},
	//载入订单json数据
	loadOrderInfo:function(){
		var selectedFile = document.getElementById("file_loadOrderInfojson").files[0];//获取读取的File对象
	    var name = selectedFile.name;//读取选中文件的文件名
	    var size = selectedFile.size;//读取选中文件的大小
	    console.log("文件名:"+name+"大小："+size);
	    var reader = new FileReader();//这里是核心！！！读取操作就是由它完成的。
	    reader.readAsText(selectedFile);//读取文件的内容
	    reader.onload = function(){
	        console.log(this.result);//当读取完成之后会回调这个函数，然后此时文件的内容存储到了result中。直接操作即可。
	        myformUtils.formFillByJson("#myformsubmit","order_",JSON.parse(this.result));
	    };
	},
	//初始就启动
	initStart:function(){
		setTimeout(function(){menubuttonIndexTool.start_interval_saveOrderInfo()},5000)
	},
	//间接触发file按钮功能
	triggle_file_loadOrderInfojson:function(){
		$("#file_loadOrderInfojson").click();
	},
	//字体修改大小
	fontsizeset:function(){
		var fontsize_str=
			`<div style='height:10px'></div>
			<div style='height:50px'>
				<input type="checkbox" id="fontsize_checkbox" checked value="order" />订单信息
			    <input type="checkbox" id="fontsize_checkbox" checked value="material" />物料仓
			    <input type="checkbox" id="fontsize_checkbox" checked value="bigmaterial" />物料大组件
			    <input type="checkbox" id="fontsize_checkbox" checked value="tag" />自定义标签
			</div>
			<input class="easyui-slider" value="16" style="width:250px" data-options="
            showTip: true,
            min:10,
            max:50,
            rule: [10,'|',20,'|',30,'|',40,'|',50],
            tipFormatter: function(value){
                return value+'px';
            },
            onChange: function(value){
            	menubuttonIndexTool.fontsizeChange(value);
            }">
            <div style='height:30px'></div>
            <h5 style="color:red">温馨提示:</br>完善订单后,最后操作字体大小调整,保存订单.</h5>`;
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '字体大小设置',
            width: "300",
            height: "200",
            closed: false,
            border:true,
            cache: false,
            content:fontsize_str,
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            }
        });
	},//字体大小改变的时候
	fontsizeChange:function(val){
		//source_factory_colresizeable_for_two??
		 $("[id='fontsize_checkbox']").each(function(){
             if($(this).is(":checked")){
            	 /*console.info($(this).val());*/
            	 if($(this).val()=="order"){
            		 $("[id^='draggable_order_info_']").each(function(){
            			 $(this).find(".right-div-comp-left-text-span").css({"font-size":val+"px","line-height":val+"px"})
            			 $(this).find("[name]").css({"font-size":val+"px","line-height":val+"px"})
            		 })
            	 }
            	 if($(this).val()=="material"){
            		 $("[id^='draggable_source_factory_']").each(function(){
            			 if($(this).find(".source_factory_colresizeable").length<1){
            				 $(this).find(".right-div-comp-left-text-span").css({"font-size":val+"px","line-height":val+"px"})
                			 $(this).find("[name]").css({"font-size":val+"px","line-height":val+"px"})
            			 }
            			 //物料大组件
            			 //$(this).find(".source_factory_colresizeable_td_div").css({"font-size":val+"px","line-height":val+"px"})
            		 })
            		 $("[id^='draggable_second_factory_']").each(function(){
            			 $(this).find(".right-div-comp-left-text-span").css({"font-size":val+"px","line-height":val+"px"})
            			 $(this).find("[name]").css({"font-size":val+"px","line-height":val+"px"})
            		 })
            		 $("[id^='draggable_info_remark_factory_']").each(function(){
            			 $(this).find(".right-div-comp-left-text-span").css({"font-size":val+"px","line-height":val+"px"})
            			 $(this).find("[name]").css({"font-size":val+"px","line-height":val+"px"})
            		 })
            	 }
            	 if($(this).val()=="bigmaterial"){
            		//物料大组件
            		 $("[id^='draggable_source_factory_']").each(function(){
            			 if($(this).find(".source_factory_colresizeable").length>0){
                			 $(this).find(".source_factory_colresizeable_td_div").css({"font-size":val+"px","line-height":val+"px"})
            			 }
            		 });
            	 }
            	 if($(this).val()=="tag"){
            		 $("[id^='draggable_label_']").each(function(){
            			 $(this).find("[name]").css({"font-size":val+"px","line-height":val+"px"})
            		 })
            	 }
             }
         });
	},
	//清除NAME逗号判断
	clear_Interval_UtilForEveryAreaEvent_allNameReplaceDH:function(){
		clearInterval(Interval_UtilForEveryAreaEvent_allNameReplaceDH);
	},
	//订单附件上传
	uploadOrderFile:function(){
		var orderNo = $("[name='order_p_order_no']").val();
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '订单附件上传',
            width: "500",
            height: "400",
            closed: false,
            border:true,
            cache: false,
            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='uploadOrderFile.html?type=2&orderNo="+orderNo+"'></iframe>",
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            }
        });
	},
	//添加物料类型
	addMaterialType:function(){
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '物料类型添加',
            width: "500",
            height: "400",
            closed: false,
            border:true,
            cache: false,
            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='addMaterialType.html'></iframe>",
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            }
        });
	},
	//通过业务单数据载入到生产订单模版
	loadDataFromBusiness:function(){
		$('div#easyui_dialog_index').dialog({
            modal: true,
            title: '物料类型添加',
            width: "500",
            height: "400",
            closed: false,
            border:true,
            cache: false,
            content:"<iframe frameborder='0' scrolling='no' style='width:99%;height:97%;' src='select_template_data_from_ywd.html'></iframe>",
            onClose : function() {  
               $("div#easyui_dialog_index").dialog('destroy');  
               $("body").append("<div id='easyui_dialog_index'></div>");
            }
        });
	}
}
if($("#printArea").length>0)menubuttonIndexTool.initStart();
var str =
	`<span id="menubutton_index">
	<a href="#" id='menubutton_index_tool'>小工具</a>
	<div id="mm1" style="width:150px;">
        <div data-options="iconCls:'icon-save'" onclick="menubuttonIndexTool.saveAsimg()">保存网页为图片</div>
    	<div data-options="iconCls:'icon-ok'" onclick="menubuttonIndexTool.start_interval_saveOrderInfo()">启用实时保存数据</div>
    	<div data-options="iconCls:'icon-cancel'" onclick="menubuttonIndexTool.clear_interval_saveOrderInfo()">禁用实时保存数据</div>
    	<div data-options="iconCls:'icon-reload'" onclick="menubuttonIndexTool.triggle_file_loadOrderInfojson()">载入实时保存数据</div>
    	<div data-options="iconCls:'icon-edit'" onclick="menubuttonIndexTool.fontsizeset()">设置字体大小</div>
    	<div data-options="iconCls:'icon-upload'" onclick="menubuttonIndexTool.uploadOrderFile()">订单附件上传</div>
    	<div data-options="iconCls:'icon-edit'" onclick="menubuttonIndexTool.addMaterialType()">物料类型添加</div>
    	<div class="menu-sep"></div>
    	<div>
            <span>开发者工具</span>
            <div>
                <div onclick="menubuttonIndexTool.clear_Interval_UtilForEveryAreaEvent_allNameReplaceDH()">清除NAME逗号判断</div>
                <div onclick="menubuttonIndexTool.saveTemplateDataAsFileNow()">即刻保存JSON数据</div>
            </div>
        </div>
        <div class="menu-sep"></div>
        <div>
            <span>业务单工具</span>
            <div>
                <div onclick="menubuttonIndexTool.loadDataFromBusiness()">载入业务单数据</div>
            </div>
        </div>
    </div>
    <input type="file" id="file_loadOrderInfojson" style="display:none" onchange="menubuttonIndexTool.loadOrderInfo();"/>
    </span>`;
$("#menubutton_index").remove();
$("#printArea").parent().append(str)
$('#menubutton_index_tool').menubutton({
    iconCls: 'icon-tip',
    menu: '#mm1'
});
$.getScript("./jquery/html2img/html2cavas.min.js")
.done(function() {
	 $.getScript("./jquery/html2img/canvas2image.js")
	 .done(function() {
		 console.info("html2cavas canvas2image success")
	});
});
//dom 底色白色避免申请 finddff影响
$("[name]").css({"background-color":"white"});
//add printArea Ajax
$("#printArea").bind("click.ajax",function(){
	$.ajax({
        type: 'POST',
        url: serverUrl+"/createOrder/operateLog",
        data: {type:1,order_no:$("[name='order_p_order_no']").val()},
        success: function(data){
          console.info("#printArea operateLog");
        }
	});
})